<template>
  <!-- 系统设备 -->
  <div class="components-promotion">
    <title-bar
      title="推广统计"
      tips-content="通过全网找邀请链接访问系统的访问量统计"
    />

    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form
        ref="test-form"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item label="邀请人:">
            <el-input
              v-model="searchForm.inviteUser"
              clearable
              placeholder="请输入昵称"
            />
          </el-form-item>

          <el-form-item label="手机号:">
            <el-input
              v-model="searchForm.mobile"
              clearable
              placeholder="请输入手机号"
            />
          </el-form-item>

          <el-form-item label="访问IP:">
            <el-input
              v-model="searchForm.ip"
              clearable
              placeholder="请输入访问IP"
            />
          </el-form-item>

          <el-form-item label="访问时间:">
            <el-date-picker
              v-model="dateRange"
              type="datetimerange"
              :range-separator="$t('date.tip')"
              :default-time="[new Date(2000, 1, 1, 0, 0, 0),
                              new Date(2000, 2, 1, 23, 59, 59)]"
              value-format="YYYY-MM-DD HH:mm:ss"
              :start-placeholder="$t('date.start')"
              :end-placeholder="$t('date.end')"
            />
          </el-form-item>

          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('shopFeature.searchBar.search') }}
            </div>
            <div
              class="default-btn"
              @click="clearSearch"
            >
              {{ $t('product.reset') }}
            </div>
            <div
              class="default-btn primary-btn"
              @click="exportVisite"
            >
              导出
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <!-- 搜索栏end -->
    <!-- 表格主体 -->
    <div class="main-container">
      <!-- 表格 -->
      <div class="table-con seckill-table">
        <el-table
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
        >
          <el-table-column
            prop="inviteUser"
            label="邀请人昵称"
            min-width="250"
          />

          <el-table-column
            prop="mobile"
            label="手机号"
            min-width="250"
          />

          <el-table-column
            prop="ip"
            label="访问IP"
            min-width="250"
          />

          <el-table-column
            prop="visitDevice"
            label="系统设备"
            min-width="250"
          />

          <el-table-column
            prop="visitTime"
            label="访问时间"
            min-width="250"
          />
        </el-table>
      </div>
      <!-- 分页 -->
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>
    <!-- 表格主体end -->
  </div>
</template>

<script setup>
import titleBar from './title-bar.vue'
import { Debounce } from '@/utils/debounce'
import { ElLoading, ElMessage } from 'element-plus'

const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const dateRange = ref([])
const searchForm = ref({})
const dataList = ref([])

const getDataList = () => {
  const queryData = {
    ...searchForm.value,
    current: page.currentPage,
    size: page.pageSize
  }
  if (dateRange.value && dateRange.value.length == 2) {
    queryData.visitTimeStart = dateRange.value[0]
    queryData.visitTimeEnd = dateRange.value[1]
  }
  http({
    url: http.adornUrl('/promotionLog/page'),
    method: 'get',
    params: http.adornParams(queryData)
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total

    // 末尾页数据为空重新请求
    if (!dataList.value.length && page.currentPage > 1) {
      page.currentPage = 1
      getDataList()
    }
  })
}

const onSearch = () => {
  page.currentPage = 1
  getDataList()
}

const clearSearch = () => {
  searchForm.value = {}
  dateRange.value = []
}

const exportVisite = Debounce(() => {
  const loading = ElLoading.service({
    lock: true,
    target: '.components-promotion',
    customClass: 'export-load',
    background: 'transparent',
    text: '导出中...'
  })
  const queryData = {
    ...searchForm.value,
    current: page.currentPage,
    size: page.pageSize
  }
  if (dateRange.value && dateRange.value.length == 2) {
    queryData.visitTimeStart = dateRange.value[0]
    queryData.visitTimeEnd = dateRange.value[1]
  }
  http({
    url: http.adornUrl('/promotionLog/exportShop'),
    method: 'get',
    params: http.adornParams(queryData),
    responseType: 'blob'
  }).then(({ data }) => {
    loading.close()
    const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' })
    const fileName = '推广统计.xlsx'
    const elink = document.createElement('a')
    if ('download' in elink) {
      elink.download = fileName
      elink.style.display = 'none'
      elink.href = URL.createObjectURL(blob)
      document.body.appendChild(elink)
      elink.click()
    } else {
      navigator.msSaveBlob(blob, fileName)
    }
    ElMessage({
      message: $t('shop.exportSuccess'),
      type: 'success',
      duration: 1500
    })
  }).catch(() => {
    loading.close()
  })
})

/**
 * 每页数量变更
 * @param val
 */
const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList()
}

/**
 * 页数变更
 * @param val
 */
const onPageChange = (val) => {
  page.currentPage = val
  getDataList()
}

onMounted(() => {
  onSearch()
})
</script>

<style lang="scss" scoped>

</style>
